<template>
  <div class="common-layout__channel">
    <el-carousel :interval="4000" type="card" height="200px">
      <el-carousel-item
        v-for="video in rankVideoHeader"
        :key="video.id"
        @click="router.toVideo(video.id)"
      >
        <div class="video-carousel">
          <div
            class="cover"
            :style="{
              'background-image':
                'url(&#34;' + commonApi.getCover(video.coverPath) + '&#34;)',
            }"
          ></div>
          <div class="footer">
            <h3>
              {{ video.title }}
            </h3>
          </div>
        </div>
      </el-carousel-item>
    </el-carousel>
    <div class="channel-rank">
      <h3 style="text-align: left; color: #6c6c6c; margin-bottom: 10px">
        {{ store().channelList[nowChannel].content }}排行榜
      </h3>
      <div class="channel-rank-button">
        <el-radio-group v-model="unUseful">
          <el-radio-button @click="getRank('daily')" label="日榜" />
          <el-radio-button @click="getRank('week')" label="周榜" />
          <el-radio-button @click="getRank('month')" label="月榜" />
        </el-radio-group>
        <el-button
          @click="router.toRank()"
          type="primary"
          link
          style="position: relative; top: 8px; left: 10px"
        >
          更多<el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
      <div class="channel-rank-videos">
        <div v-for="item in rankVideos.slice(0, 5)" :key="item.id">
          <VideoCard1 :video-info="item"></VideoCard1>
        </div>
      </div>
    </div>

    <div class="channel-list">
      <h3
        style="
          text-align: left;
          color: #6c6c6c;
          margin-bottom: 10px;
          margin-top: 40px;
        "
      >
        更多看点
      </h3>
      <div class="refresh__button">
        <el-button @click="refresh" type="primary" link :icon="RefreshLeft"
          >刷新</el-button
        >
      </div>
      <RecommendVideos
        ref="recommendVideos"
        :channelId="nowChannel"
      ></RecommendVideos>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, watch, onMounted, onUnmounted } from "vue";
import { useRoute } from "vue-router";
import videoApi from "@/api/video.js";
import RecommendVideos from "@/components/RecommendVideos.vue";
import VideoCard1 from "@/components/VideoCard1.vue";
import router from "@/router/router.js";
import commonApi from "@/api/common.js";
import store from "@/store/store.js";
import { RefreshLeft, ArrowRight } from "@element-plus/icons-vue";
import { Video, VideoRankType } from "@/types/video";

const route = useRoute();
const nowChannel = ref<number>(3);
const rankVideoHeader = ref<Video[]>([]);
const rankVideos = ref<Video[]>([]);
const recommendVideos = ref<Video[]>();
const unUseful = ref<string>("日榜");

const getRank = (type: VideoRankType) => {
  videoApi.getRank(type, nowChannel.value, 6).then((res) => {
    if (res.data.code === 0) {
      rankVideos.value = res.data.data;
    }
  });
};

// 初始化排行榜数据
const getRankVideo = () => {
  videoApi.getRank("daily", nowChannel.value, 6).then((res) => {
    if (res.data.code === 0) {
      rankVideoHeader.value = res.data.data;
      rankVideos.value = res.data.data;
    }
  });
};

const refresh = () => {
  // @ts-ignore
  recommendVideos.value?.refreshVideos();
};

const watchUrlParam = watch(
  () => route.params.id,
  (val) => {
    nowChannel.value = Number(val);
    getRankVideo();
  },
  { immediate: true }
);

onMounted(() => {});

onUnmounted(() => {
  watchUrlParam();
});
</script>

<style lang="scss" scoped>
.common-layout__channel {
  /*走马灯*/
  .el-carousel {
    margin: 0 auto;
    width: 80%;
    /*aspect-ratio: 8 / 3;*/
    & > .el-carousel__container {
      &,
      & > .el-carousel__item,
      & > .el-carousel__item > .video-carousel {
        border-radius: 10px;
        overflow: hidden;
        height: 100%;
      }
    }
  }

  .video-carousel {
    & > .cover {
      height: 100%;
      background-repeat: no-repeat;
      background-size: cover;
      background-position: center center;
    }
    & > .footer {
      position: absolute;
      width: 100%;
      bottom: 0;
      height: 50px;
      background: linear-gradient(
        rgb(0, 0, 0, 0),
        rgb(0, 0, 0, 0.2),
        rgb(0, 0, 0, 0.4),
        rgb(0, 0, 0, 0.6)
      );
      h3 {
        color: #ffffff;
        opacity: 0.75;
        text-align: left;
        position: relative;
        top: 20px;
        left: 15px;
      }
    }
  }
  .channel-rank {
    position: relative;

    & > div:last-child {
      display: grid;
      grid-template-columns: repeat(5, auto);
      margin-top: 20px;
    }

    .channel-rank-button {
      position: absolute;
      top: -3px;
      left: 100px;

      & > span {
        margin-left: 10px;
      }
    }

    .channel-list {
      position: relative;

      .refresh__button {
        position: absolute;
        top: 2px;
        left: 100px;
      }
    }
  }
}
</style>
